<template>
  <van-button
    v-if="isfollowed"
    @click="follows"
    :loading="loading"
    class="follow-btn"
    round
    size="small"
    >已关注</van-button
  >
  <van-button
    v-else
    @click="follows"
    :loading="loading"
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    >关注</van-button
  >
</template>

<script>
import {addfollowings,delfollowings} from '@/api/user.js'
export default {
  data() {
    return {
      loading: false,
    };
  },
  model:{
  prop:'isfollowed',
  event:'updateFollwoed'
  },
  props:{
    followedId:{
      type:[Number,String,Object],
      required:true,
    },
    isfollowed:{
     type:Boolean,
     required:true,
    }
  },
  methods:{
    async follows(){
    this.loading =true
     try {
    if(this.isfollowed){
      await delfollowings(this.followedId)
    }else{
      await addfollowings(this.followedId)
    }
    this.$emit('updateFollwoed',!this.isfollowed)
     } catch (error) {
      let message ='关注失败'
      if(error.response && error.response.status ===400){
      message= '你不能关注自己'
      }
      this.$toast(message)
     }
     this.loading =false
    }
  }
};
</script>

<style>
</style>